/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.valhalla.mailcheck.utils;

import java.util.HashMap;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.SourceDataLine;

/**
 *
 * @author guillaume
 */
public class SoundPlayer extends Thread {

    AudioInputStream audioInputStream;
    AudioFormat audioFormat;
    SourceDataLine sourceDataLine;

    public static final String SOUND_DEFAULT = "/fr/valhalla/mailcheck/resources/audio/claves.wav";
    public static final String SOUND_MAIL_1 = "/fr/valhalla/mailcheck/resources/audio/mail_1.wav";
    public static final String SOUND_MAIL_2 = "/fr/valhalla/mailcheck/resources/audio/mail_2.wav";
    public static final String SOUND_MAIL_3 = "/fr/valhalla/mailcheck/resources/audio/mail_3.wav";
    public static final String SOUND_ALARM = "/fr/valhalla/mailcheck/resources/audio/alarm.wav";
    public static final String SOUND_BIRD_1 = "/fr/valhalla/mailcheck/resources/audio/bird_1.wav";
    public static final String SOUND_BIRD_2 = "/fr/valhalla/mailcheck/resources/audio/bird_2.wav";
    public static final String SOUND_CLAVES = "/fr/valhalla/mailcheck/resources/audio/claves.wav";
    public static final String SOUND_COMPUTER = "/fr/valhalla/mailcheck/resources/audio/computer.wav";
    public static final String SOUND_COWBELL = "/fr/valhalla/mailcheck/resources/audio/cowbell.wav";
    public static final String SOUND_WHISTLE_1 = "/fr/valhalla/mailcheck/resources/audio/whistle_1.wav";
    public static final String SOUND_WHISTLE_2 = "/fr/valhalla/mailcheck/resources/audio/whistle_2.wav";

    public HashMap<String, String> SOUNDS = new HashMap<String, String>();
    
    public SoundPlayer() throws Exception {
        this(SOUND_DEFAULT);
    }

    public SoundPlayer(String sound) throws Exception {
        audioInputStream = AudioSystem.getAudioInputStream(getClass().getResource(sound));
        audioFormat = audioInputStream.getFormat();
        DataLine.Info dataLineInfo = new DataLine.Info(SourceDataLine.class, audioFormat);
        sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo);

        SOUNDS.put("Mail 1", SOUND_MAIL_1);
        SOUNDS.put("Mail 2", SOUND_MAIL_2);
        SOUNDS.put("Mail 3", SOUND_MAIL_3);
        SOUNDS.put("Alarme", SOUND_ALARM);
        SOUNDS.put("Oiseau 1", SOUND_BIRD_1);
        SOUNDS.put("Oiseau 2", SOUND_BIRD_2);
        SOUNDS.put("Click", SOUND_CLAVES);
        SOUNDS.put("Ordinateur", SOUND_COMPUTER);
        SOUNDS.put("Toc", SOUND_COWBELL);
        SOUNDS.put("Sifflet 1", SOUND_WHISTLE_1);
        SOUNDS.put("Sifflet 2", SOUND_WHISTLE_2);
    }

    @Override
    public void run() {
        try {
            byte tempBuffer[] = new byte[10000];
            sourceDataLine.open(audioFormat);
            sourceDataLine.start();

            int cnt;
            while ((cnt = audioInputStream.read(
                    tempBuffer, 0, tempBuffer.length)) != -1) {
                if (cnt > 0) {
                    sourceDataLine.write(tempBuffer, 0, cnt);
                }
            }
            sourceDataLine.drain();
            sourceDataLine.close();
        } catch(Exception e) {e.printStackTrace();}
    }

    public void play() {
        run();
    }

}
